package com.study.d012Components.listview;

import java.util.Comparator;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.ListView.EditEvent;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
*@description 自定义ListView
*步骤：
*
*
*
*@author xtayhame
*date 2020年3月13日
*version
*since v1.0
*/
public class ListViewWithCustomize extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		//ObservableList<Student2> datas=FXCollections.observableArrayList();

		//通过这种方式获取的列表，在进行set数据时，不需要进行refresh()方法进行刷新
		ObservableList<Student2> datas = FXCollections.observableArrayList(new Callback<Student2, Observable[]>() {
			@Override
			public Observable[] call(Student2 param) {
				Observable[]  osb = new Observable[]{param.getAge(),param.getName()};
				return osb;
			}
		});

		Student2 s1 = new Student2("张三",18);
		Student2 s2 = new Student2("李四",19);
		Student2 s3 = new Student2("王五",30);
		Student2 s4 = new Student2("赵柳",55);





		datas.addAll(s1,s2,s3,s4);

		Button b1  = new Button("点击");
		ListView<Student2> listView = new ListView<>(datas);
		listView.setPrefSize(200, 300);
		anchorPane.getChildren().addAll(b1,listView);
		AnchorPane.setLeftAnchor(listView, 50.0);
		AnchorPane.setTopAnchor(listView, 50.0);

		stage.setScene(new Scene(anchorPane, 400, 500));
		stage.show();



		listView.setEditable(true);
		listView.setCellFactory(new Callback<ListView<Student2>, ListCell<Student2>>() {
			int index=0;   //获取当前编辑的索引
			Student2 stu;  //获取当前编辑的对象
			ListCell<Student2> tempListCell;

			@Override
			public ListCell<Student2> call(ListView<Student2> param) {

				//获取索引
				param.setOnEditStart(new EventHandler<ListView.EditEvent<Student2>>() {
					@Override
					public void handle(EditEvent<Student2> event) {
						System.out.println("当前编辑的索引： " + event.getIndex());
						index = event.getIndex();
						stu = param.getItems().get(index);
					}
				});


				ListCell<Student2> listCell = new ListCell<Student2>(){

					//开始编辑
					@Override
					public void startEdit() {
						tempListCell = this;

						//这个一定要保留才行
						super.startEdit();

						System.out.println("开始编辑");
						//将label变成textfield，用于输入， 首先需要获取当前点击的索引
						HBox hBox = new HBox();

						ImageView iv = new ImageView(new Image("file:E:\\javafxworkspace\\javafx_study\\src\\icon\\01.jpg"));
						iv.setPreserveRatio(true);
						iv.setFitWidth(20);

						Button b1 = new Button(stu.getName().get() + "-button");

						TextField nameField = new TextField(stu.getName().get());
						nameField.setPrefWidth(40);

						TextField ageField = new TextField(stu.getAge().get()+"");
						ageField.setPrefWidth(40);

						hBox.getChildren().addAll(iv,b1,nameField,ageField);

						this.setGraphic(hBox);

						//还可以给按钮添加事件

						b1.setOnAction(new EventHandler<ActionEvent>() {

							@Override
							public void handle(ActionEvent event) {
								System.out.println(((Button)event.getSource()).getText());
							}
						});



						//添加回车事件
						nameField.setOnKeyPressed(new EventHandler<KeyEvent>() {

							@Override
							public void handle(KeyEvent event) {
								if(event.getCode().getName().equals(KeyCode.ENTER.getName())){
									//如果是回车
									String newname = nameField.getText();
									String newage = ageField.getText();
									stu.setName(newname);
									stu.setAge(Integer.parseInt(newage));
									tempListCell.commitEdit(stu);
								}
							}
						});

						ageField.setOnKeyPressed(new EventHandler<KeyEvent>() {

							@Override
							public void handle(KeyEvent event) {
								if(event.getCode().getName().equals(KeyCode.ENTER.getName())){
									//如果是回车
									String newname = nameField.getText();
									String newage = ageField.getText();
									stu.setName(newname);
									stu.setAge(Integer.parseInt(newage));
									tempListCell.commitEdit(stu);
								}
							}
						});


					}





					//提交编辑
					@Override
					public void commitEdit(Student2 newValue) {
						System.out.println("新的值："+newValue);
						super.commitEdit(newValue);
					}


					//取消编辑
					@Override
					public void cancelEdit() {
						super.cancelEdit();
						HBox hBox = new HBox();

						ImageView iv = new ImageView(new Image("file:E:\\javafxworkspace\\javafx_study\\src\\icon\\01.jpg"));
						iv.setPreserveRatio(true);
						iv.setFitWidth(20);

						Button b1 = new Button(stu.getName().get() + "-button");

						Label nameLabel = new Label(stu.getName().get());

						Label ageLabel = new Label(stu.getAge().get()+"");

						hBox.getChildren().addAll(iv,b1,nameLabel,ageLabel);

						this.setGraphic(hBox);

						//还可以给按钮添加事件

						b1.setOnAction(new EventHandler<ActionEvent>() {

							@Override
							public void handle(ActionEvent event) {
								System.out.println(((Button)event.getSource()).getText());
							}
						});

					}


					//自定义显示方式
					@Override
					protected void updateItem(Student2 item, boolean empty) {
						super.updateItem(item, empty);
						if(!empty){
							HBox hBox = new HBox();

							ImageView iv = new ImageView(new Image("file:E:\\javafxworkspace\\javafx_study\\src\\icon\\01.jpg"));
							iv.setPreserveRatio(true);
							iv.setFitWidth(20);

							Button b1 = new Button(item.getName().get() + "-button");

							Label nameLabel = new Label(item.getName().get());

							Label ageLabel = new Label(item.getAge().get()+"");

							hBox.getChildren().addAll(iv,b1,nameLabel,ageLabel);

							this.setGraphic(hBox);

							//还可以给按钮添加事件

							b1.setOnAction(new EventHandler<ActionEvent>() {

								@Override
								public void handle(ActionEvent event) {
									System.out.println(((Button)event.getSource()).getText());
								}
							});
						}

					}
				};




				return listCell;
			}
		});


	}

}
